import { requestApi } from "../request";
import Taro from "@tarojs/taro";
const baseUrl =
  process.env.TARO_APP_BASE_URL || "https://car-admin.xipeisoft.com";

const AuthUtil = {
  login: (data) => {
    return requestApi({
      url: "/api/wechat-mini/login",
      method: "POST",
      data,
      custom: {
        showSuccess: true,
        loadingMsg: "登录中",
        successMsg: "登录成功",
      },
    });
  },

  // 登出系统
  logout: () => {
    return requestApi({
      url: "/member/auth/logout",
      method: "POST",
    });
  },
  // 刷新令牌
  refreshToken: (refreshToken) => {
    return requestApi({
      url: "/member/auth/refresh-token",
      method: "POST",
      params: {
        refreshToken,
      },
      custom: {
        loading: false, // 不用加载中
        showError: false, // 不展示错误提示
      },
    });
  },

  // 微信小程序的一键登录
  weixinMiniAppLogin: (phoneCode, loginCode, state) => {
    return requestApi({
      url: "/api/strapi-wechat-miniprogram-auth/login",
      method: "POST",
      data: {
        phoneCode,
        loginCode,
        state,
      },
    });
  },

  // 创建微信 JS SDK 初始化所需的签名
  createWeixinMpJsapiSignature: (url) => {
    return requestApi({
      url: "/member/auth/create-weixin-jsapi-signature",
      method: "POST",
      params: {
        url,
      },
      custom: {
        showError: false,
        showLoading: false,
      },
    });
  },

  // https://car-admin.xipeisoft.com/api/upload
  createOrder: (param) => {
    console.log(param, "param");
    return requestApi({
      url: "/api/pay-orders",
      method: "POST",
      data: {
        data: param,
      },
      custom: {
        loading: true, // 不用加载中
        showError: true, // 不展示错误提示
      },
    });
  },
  // 身份证识别接口
  ocrIdCardRecognition: (imageBase64, type) => {
    const url = "/ocr";
    const requestData = {
      image: imageBase64,
      id_card_side: type === "idFront" ? "front" : "back", // 根据正反面设置
    };

    return requestApi({
      url: url,
      method: "POST",
      data: requestData,
      header: {
        "Content-Type": "application/json",
      },
    });
  },
  // 驾驶证识别接口
  ocrDrivingLicenseRecognition: (imageBase64, type) => {
    const url = "https://chf.lven.tech/api/ocr/vehicle_license";
    const requestData = {
      image: imageBase64,
      side: type === "drivingFront" ? "front" : "back", // 根据正反面设置
    };

    return requestApi({
      url: url,
      method: "POST",
      data: requestData,
      header: {
        "Content-Type": "application/json",
      },
    });
  },

  // 单个图片上传
  uploadFile: (filePath) => {
    return new Promise((resolve, reject) => {
      Taro.uploadFile({
        url: baseUrl + "/api/upload",
        filePath: filePath,
        name: "files",
        header: {
          Accept: "application/json",
          Authorization: `Bearer ${Taro.getStorageSync("token")}`, // 带上 token
        },
        formData: {
          // 如果需要附加额外表单字段，可以在这里添加
        },
        success: (res: any) => {
          if (res.statusCode === 200) {
            console.log("上传成功", res);
            resolve(JSON.parse(res.data)[0]);
          } else {
            Taro.showToast({
              title: `上传失败: ${res.statusCode}`,
              icon: "none",
            });
            resolve(JSON.parse(res.data));
          }
        },
        fail: (error) => {
          // 上传失败处理
          console.error("上传失败", error);
          Taro.showToast({
            title: "上传失败",
            icon: "none",
          });
          reject(error);
        },
      });
    });
  },

  // 数据提交接口
  submitForm: (param) => {
    return requestApi({
      url: "/api/track-orders",
      method: "POST",
      data: {
        ...param,
      },

      custom: {
        showSuccess: true,
        loadingMsg: "上传中",
        successMsg: "上传成功",
      },
    });
  },
  // 数据列表接口
  getOrder: (page) => {
    return requestApi({
      url: `/api/track-orders`,
      method: "get",
      data: {
        "pagination[page]": page,
        "pagination[pageSize]": 25,
        "sort[0]": "createdAt:desc",
        "sort[1]": "updatedAt:desc",
      },

      custom: {
        showSuccess: true,
        loadingMsg: "上传中",
        successMsg: "上传成功",
      },
    });
  },
  getForm: (id) => {
    return requestApi({
      url: `/api/track-orders/${id}`,
      method: "GET",
      custom: {
        loadingMsg: "正在加载订单详情...",
        successMsg: "订单详情加载成功",
      },
    });
  },
  // 数据提交接口
  editForm: (param, id) => {
    return requestApi({
      url: `/api/track-orders/${id}`,
      method: "PUT",
      data: {
        ...param,
        orderStatus: 2,
      },

      custom: {
        showSuccess: true,
        loadingMsg: "上传中",
        successMsg: "上传成功",
      },
    });
  },
  payOrders: () => {
    return requestApi({
      url: `/api/pay-orders`,
      method: "GET",

      custom: {
        showSuccess: true,
        loadingMsg: "上传中",
        successMsg: "上传成功",
      },
    });
  },
  userInfo: () => {
    return requestApi({
      url: `/api/users/me`,
      method: "GET",
      custom: {
        showSuccess: true,
        loadingMsg: "上传中",
        successMsg: "上传成功",
      },
    });
  },

  EidResult: (param) => {
    return requestApi({
      url: `/api/eid-face/eid-result`,
      method: "POST",
      data: {
        ...param,
      },
      custom: {
        showSuccess: true,
        loadingMsg: "上传中",
        successMsg: "上传成功",
      },
    });
  },
  EidToken: () => {
    return requestApi({
      url: `/api/eid-face/eid-token`,
      method: "POST",
      custom: {
        showSuccess: true,
        loadingMsg: "上传中",
        successMsg: "上传成功",
      },
    });
  },

  //
};

export default AuthUtil;
